package com.jacobson.huobi.components.toptab;

import android.util.SparseArray;
import android.util.SparseIntArray;

import java.util.HashMap;
import java.util.Map;

/**
 * @author WuChenZhong on 2019/7/21 14:43
 * @version 1.0.0
 */
public class TabsData {
    private boolean hasData = false;
    private int size = 0;
    private SparseIntArray levelIndexMapping;
    private Map<String, Integer> tabIndexMapping;
    private SparseArray<String> levelTabMapping;
    private SparseArray<String> indexTabMapping;

    public TabsData() {
        levelIndexMapping = new SparseIntArray();
        tabIndexMapping = new HashMap<>();
        levelTabMapping = new SparseArray<>();
        indexTabMapping = new SparseArray<>();
    }

    public synchronized void add(int level, String tab) {
        hasData = true;
        final int position = size++;
        levelIndexMapping.put(level, position);
        tabIndexMapping.put(tab, position);
        levelTabMapping.put(level, tab);
        indexTabMapping.put(position, tab);
    }

    public int size() {
        return size;
    }

    public boolean hasTab() {
        return hasData && (size > 0);
    }

    public String getTab(int position) {
        return indexTabMapping.get(position);
    }

    public boolean hasLevel(int level) {
        return levelIndexMapping.indexOfKey(level) >= 0;
    }

    public int getPositionByIndex(int level) {
        Integer value = levelIndexMapping.get(level);
        return value == null ? -1 : value;
    }

    @Override
    public String toString() {
        return "TabsData{" +
                "hasData=" + hasData +
                ", size=" + size +
                ", levelIndexMapping=" + levelIndexMapping +
                ", tabIndexMapping=" + tabIndexMapping +
                ", levelTabMapping=" + levelTabMapping +
                ", indexTabMapping=" + indexTabMapping +
                '}';
    }
}
